package com.bib.ciglog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.bib.ciglog.data.CigObj;
import com.bib.ciglog.data.DaoProvider;
import com.bib.ciglog.data.Data;

public class MainActivity extends Activity {

	
	ArrayAdapter<CigObj> adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Data d = DaoProvider.getData();
		d.setExtraProp(Data.XTRA_FILE_PATH, getApplicationContext().getFilesDir().getAbsolutePath());

		System.out.println("Data initialized");
		
//		adapter = new ArrayAdapter<CigObj>(this, R.layout.list_item,R.id.cigItem, d.getCigs());
		adapter = new CigArrayAdapter(this, d.getCigs());
		
		ListView lv = (ListView)findViewById(R.id.cigList);
		lv.setAdapter(adapter);
//		Utils.setListViewHeightBasedOnChildren(lv, Const.LIST_MAX_ITEMS);
		
//		ImageButton supprButton = (ImageButton)findViewById(R.id.imgDelCig);
//		supprButton.setImageResource(R.drawable.ic_action_cancel);
		adapter.notifyDataSetChanged();
	}
	
	
	


	public void addCig(View view){
		addCig(CigObj.REGULAR_CIG);
	}

	public void addECig(View view){
		addCig(CigObj.E_CIG);
	}
	
	public void addCig(int type){
		DaoProvider.getData().addCig(new CigObj(type,System.currentTimeMillis()));
//		Utils.setListViewHeightBasedOnChildren((ListView)findViewById(R.id.cigList), Const.LIST_MAX_ITEMS);
		adapter.notifyDataSetChanged();
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle presses on the action bar items
		switch (item.getItemId()) {
//		case R.id.action_view_as_list:
//			openLogs();
//			return true;
		case R.id.action_refresh:
			refreshData();
			return true;
			
		case R.id.action_clear_data:
			clearData();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	

	

	private void clearData() {
		
		new AlertDialog.Builder(this)
	    .setTitle("Clear data")
	    .setMessage("Are you sure you want to clear data?")
	    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int which) { 
	        	DaoProvider.getData().clearData();
	    		adapter.notifyDataSetChanged();
	        }
	     })
	    .setNegativeButton("No", new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int which) { 
	            // do nothing
	        }
	     })
	     .show();
		
		
	}
	

	
	//When clicking
	public void delCig(View v) {
		System.out.println("DELCIG");
		//TODO
		
	}


	@Override
	protected void onDestroy() {
		System.out.println("DESTROY");
		DaoProvider.getData().save();
		super.onDestroy();
	}

	

	private void refreshData() {
		System.out.println("refreshData");
		adapter.notifyDataSetChanged();

	}

	private void openLogs() {
		System.out.println("LOGS");
		//		Intent intent = new Intent(this, ArrayLogsActivity.class);
		//		
		//		startActivity(intent);
	}
}